package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.UserExt;
import com.ejuornal.dao.UserExtDao;

@Component("userExtDao")
public class UserExtDaoImpl extends DaoSupport<UserExt> implements UserExtDao {

	public Page<UserExt> findByProperty(int current_record, String url,
			Object property, Object value) {
		Page<UserExt> page = null;
		if (property == "" || property == null || value == "" || value == null) {
			page = find(current_record, url);
		} else {
			String hsql = "where " + property + " = ? ";
			page = find(current_record, url, hsql, new Object[] { value });
		}

		return page;
	}

	public UserExt getDetailByUserId(int userId) {
		UserExt userExt = null;
		String sql = "select * from t_user_ext where userId = ?";
		userExt = super.getJdbcTemplate().queryForObject(sql,
				new Object[] { userId }, new UserExtDaoMapper());
		return userExt;
	}
	
	public boolean isUserExtExist(int userId) {
		String sql = "select count(*) from t_user_ext where userId=?";
		@SuppressWarnings("deprecation")
		int result = super.getJdbcTemplate().queryForInt(sql, new Object[]{ userId });
		
		if(result > 0) {
			return true;
		} else {
			return false;
		}
	}
}

final class UserExtDaoMapper implements RowMapper<UserExt> {

	public UserExt mapRow(ResultSet rs, int num) throws SQLException {
		UserExt userExt = new UserExt();
		userExt.setUserId(rs.getInt("userId"));
		userExt.setUserExtId(rs.getInt("userExtId"));
		userExt.setTel(rs.getString("tel"));
		userExt.setSex(rs.getString("sex"));
		userExt.setScore(rs.getInt("score"));
		userExt.setRealName(rs.getString("realName"));
		userExt.setRank(rs.getString("rank"));
		userExt.setPosition(rs.getString("position"));
		userExt.setPenName(rs.getString("panName"));
		userExt.setHobby(rs.getString("hobby"));
		userExt.setHeadPic(rs.getString("headPic"));
		userExt.setDegree(rs.getString("degree"));
		userExt.setCompany(rs.getString("company"));
		return userExt;
	}
}
